#include "TextManager.h"


std::vector<std::string>TextManager::log = *new std::vector<std::string>();
SDL_Surface* TextManager::logSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, TileManager::SCREEN_WIDTH , TileManager::GUI_HEIGHT, 32,  0xff000000,  0x00000000,  0x00000000,  0x000000ff);;
bool TextManager::needsUpdate = false;
TextManager::TextManager(void)
{
}


TextManager::~TextManager(void)
{
}

void TextManager::Message(std::string text)
{
	log.push_back(text);
	if(log.size()>10)
	{
		log.erase(log.begin(), log.end()-10);
	}
	TextManager::needsUpdate = true;
}


void TextManager::update()
{
	if(needsUpdate)
	{
	SDL_FillRect( logSurface, &logSurface->clip_rect, SDL_MapRGB( logSurface->format, 0x00, 0x00, 0x00 ) );	
	if(log.empty())
		return;
	int textY = logSurface->clip_rect.h -24;
	std::vector<std::string>::reverse_iterator it;
	std::vector<std::string>::reverse_iterator end;
	
	it = log.rbegin();

	if(log.size()>10)
	{
		end = log.rbegin()+9;
	}else
	{
		end = log.rend();
	}
	while(it<=end)
	{
		
		Draw::putTextAt(6,textY, *it, logSurface);
		textY-=18;
		it++;

	}
	
	Draw::apply_surface(0, TileManager::SCREEN_HEIGHT-TileManager::GUI_HEIGHT, logSurface);
	needsUpdate = false;
	}
	
	//free(&it);
//	free(&end);
}